gopls: prevent crash in ExtractToNewFile when bounds are invalid#645
gopls: prevent crash in ExtractToNewFile when bounds are invalid#645AmSach wants to merge 1 commit into
Conversation
The bounds checks in ExtractToNewFile called bug.Reportf but then continued execution, leading to a panic when slicing pgf.Src with invalid bounds. Since CheckPos calls safetoken.Offset which can return an error that is ignored, the subsequent slice pgf.Src[start-fileStart:end-fileStart] at line 126 could crash with 'slice bounds out of range'. Fix by replacing bug.Reportf with return nil, bug.Errorf so the function exits safely with an error instead of crashing. Also fix the comparison 0 <= start-fileStart which requires casting token.Pos difference to int. Fixes golang/go#70553
|
This PR (HEAD: c413f88) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/tools/+/770980. Important tips:
|
|
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/770980. |
|
Message from Gopher Robot: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be Please don’t reply on this GitHub thread. Visit golang.org/cl/770980. |
|
Message from Alan Donovan: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/770980. |
Fix for golang/go#70553
Problem
In
ExtractToNewFile, the bounds checks at lines 118-124 calledbug.Reportfbut then continued execution. Whensafetoken.Offsetreturned an error that was ignored byCheckPos, the subsequent slicepgf.Src[start-fileStart:end-fileStart]would panic with "slice bounds out of range".Fix
Replace
bug.Reportfwithreturn nil, bug.Errorfso the function exits safely with an error instead of crashing.Also fix the comparison
0 <= start-fileStartwhich requires casting thetoken.Posdifference toint.Testing
Go syntax check passes. The fix ensures bounds checks guard the slice operation.
Issue Reference
Fixes golang/go#70553